/**
 * 
 */
package com.somobile.services.impl;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.JsonDeserializer;
import com.somobile.beans.Question;
import com.somobile.connection.ISoUrlProvider;
import com.somobile.json.JSONHelper;
import com.somobile.results.beans.QuestionResults;
import com.somobile.services.IQuestionService;
import com.somobile.types.converters.StandardDeserializers;

/**
 * @author Jatan
 *
 */

@Service
public class QuestionService implements IQuestionService
{

	@Autowired
	ISoUrlProvider urlProvider;
	
	
	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getActiveQuetions()
	 */
	@Override
	public QuestionResults getActiveQuetions()
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(urlProvider.formRequestURL("questions",new String[][]{{"sort","active"}}), QuestionResults.class,StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
		
		return questions;
	}

	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getFeaturedQuestions()
	 */
	@Override
	public QuestionResults getFeaturedQuestions()
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(urlProvider.formRequestURL("questions",new String[][]{{"sort","featured"}}), QuestionResults.class,StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
		
		return questions;
	}

	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getHotQuestions()
	 */
	@Override
	public QuestionResults getHotQuestions()
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(urlProvider.formRequestURL("questions",new String[][]{{"sort","hot"}}), QuestionResults.class,StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
		
		return questions;
	}

	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getNewestQuestions()
	 */
	@Override
	public QuestionResults getNewestQuestions()
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(urlProvider.formRequestURL("questions",new String[][]{{"sort","newest"}}), QuestionResults.class,StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
		
		return questions;
	}

	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getQuestion(long)
	 */
	@Override
	public Question getQuestion(long questionId)
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(
					urlProvider.formRequestURL("questions/"+questionId,new String[][]{{"body","true"}}), 
					QuestionResults.class, StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return questions.getQuestions()[0];
	}

	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getUnAnsweredQuestions()
	 */
	@Override
	public QuestionResults getUnAnsweredQuestions()
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(urlProvider.formRequestURL(urlProvider.formRequestURL("questions",new String[][]{{"sort","unanswered"}})), QuestionResults.class,StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return questions;
	}

	/* (non-Javadoc)
	 * @see com.somobile.services.IQuestionService#getVoteQuestions()
	 */
	@Override
	public QuestionResults getVoteQuestions()
	{
		QuestionResults questions=null;
		try
		{
			questions = (QuestionResults) JSONHelper.readObject(urlProvider.formRequestURL("questions",new String[][]{{"sort","votes"}}), QuestionResults.class,StandardDeserializers.getInstance().getDeserializers());
		}
		catch (IOException e)
		{
			
			e.printStackTrace();
		}
		
		return questions;
	}

}
